package poker.gui;

import java.awt.Component;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class PokerFrameEventListener implements MouseListener {

	MyCursorContainer cursorContainer = null;
	
	/*FROM MULTITOUCH CLASS*/
	FingerContainer fingerContainer = null; 
	private boolean isFingerDraggingModeActive = false;
	private int indexOfFingerUnderMouse = -1;
	private int mouse_x, mouse_y;
	public static final int TOUCH_EVENT_DOWN = 0;
	public static final int TOUCH_EVENT_MOVE = 1;
	public static final int TOUCH_EVENT_UP = 2;

	private GraphicsWrapper gw = null;

	public PokerFrameEventListener(){
		fingerContainer = new FingerContainer();
		gw = new GraphicsWrapper();
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public void mouseEntered(MouseEvent e) {
		if(e.getSource().getClass().equals(JPanel.class)){
			if(((JPanel) e.getSource()).getName().equals("player1InternalPanel")){
//				System.out.println("Entered panel 1 internal panel");
			}
			else if(((JPanel) e.getSource()).getName().equals("player2InternalPanel")){
//				System.out.println("Entered panel 2 internal panel");
			}
			else if(((JPanel) e.getSource()).getName().equals("player3InternalPanel")){
//				System.out.println("Entered panel 3 internal panel");
			}
			else if(((JPanel) e.getSource()).getName().equals("player4InternalPanel")){
//				System.out.println("Entered panel 4 internal panel");
			}
		
		}
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent e) {
		System.out.println("mouse pressed detected");
		
		mouse_x = e.getX();
		mouse_y = e.getY();
		
		if ( e.isControlDown() ) {
			if ( indexOfFingerUnderMouse == -1 ) {
				retrieveAndProcessFinger(e,TOUCH_EVENT_DOWN);
			}
			isFingerDraggingModeActive = true;
		}
		else {
			if(e.getSource().getClass().equals(JPanel.class)){
				System.out.println("detected panel"+((JPanel) e.getSource()).getName());
				if(((JPanel) e.getSource()).getName().equals("cardZoneP1")){//cardZoneP1
					PokerFrame.cardZoneP1Enabled = !PokerFrame.cardZoneP1Enabled;
					System.out.println("cardZoneP1 "+PokerFrame.cardZoneP1Enabled);
				}
				else if(((JPanel) e.getSource()).getName().equals("cardZoneP2")){//cardZoneP2
					PokerFrame.cardZoneP2Enabled = !PokerFrame.cardZoneP2Enabled;
					System.out.println("cardZoneP2 "+PokerFrame.cardZoneP2Enabled);
				}
				else if(((JPanel) e.getSource()).getName().equals("cardZoneP3")){//cardZoneP3
					PokerFrame.cardZoneP3Enabled = !PokerFrame.cardZoneP3Enabled;
					System.out.println("cardZoneP3 "+PokerFrame.cardZoneP2Enabled);
				}
				else if(((JPanel) e.getSource()).getName().equals("cardZoneP4")){//cardZoneP4
					PokerFrame.cardZoneP4Enabled = !PokerFrame.cardZoneP4Enabled;
					System.out.println("cardZoneP4 "+PokerFrame.cardZoneP4Enabled);
				}
			}
			else if(e.getSource().getClass().equals(JButton.class)){
				System.out.println("detected button"+((JButton) e.getSource()).getName());
				if(((JButton) e.getSource()).getName().endsWith("P1")){//buttonCard1P1
					if(((JButton) e.getSource()).getName().equals("buttonCard1P1") && PokerFrame.cardZoneP1Enabled){
						System.out.println("revealing "+((JButton) e.getSource()).getName());
					}
					else if(((JButton) e.getSource()).getName().equals("buttonCard2P1") && PokerFrame.cardZoneP1Enabled){
						System.out.println("revealing "+((JButton) e.getSource()).getName());
					}
				}
				else if(((JButton) e.getSource()).getName().endsWith("P2")){//buttonCard2P1
					if(((JButton) e.getSource()).getName().equals("buttonCard1P2") && PokerFrame.cardZoneP2Enabled){
						System.out.println("revealing "+((JButton) e.getSource()).getName());
					}
					else if(((JButton) e.getSource()).getName().equals("buttonCard2P2") && PokerFrame.cardZoneP2Enabled){
						System.out.println("revealing "+((JButton) e.getSource()).getName());
					}
				}
				else if(((JButton) e.getSource()).getName().endsWith("P3")){//buttonCard1P2
					if(((JButton) e.getSource()).getName().equals("buttonCard1P3") && PokerFrame.cardZoneP3Enabled){
						System.out.println("revealing "+((JButton) e.getSource()).getName());
					}
					else if(((JButton) e.getSource()).getName().equals("buttonCard2P3") && PokerFrame.cardZoneP3Enabled){
						System.out.println("revealing "+((JButton) e.getSource()).getName());
					}
				}
				else if(((JButton) e.getSource()).getName().endsWith("P4")){//buttonCard2P2
					if(((JButton) e.getSource()).getName().equals("buttonCard1P4") && PokerFrame.cardZoneP4Enabled){
						System.out.println("revealing "+((JButton) e.getSource()).getName());
					}
					else if(((JButton) e.getSource()).getName().equals("buttonCard2P4") && PokerFrame.cardZoneP4Enabled){
						System.out.println("revealing "+((JButton) e.getSource()).getName());
					}
				}
			}
		}
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		//do not erase
//		System.out.println("mouse released detected");
//		if(e.getSource().getClass().equals(JPanel.class)){
//			System.out.println("detected panel" + ((JPanel) e.getSource()).getName());
//			if(((JPanel) e.getSource()).getName().equals("cardZoneP1")){//cardZoneP1
//				PokerFrame.cardZoneP1Enabled = false;
//				System.out.println("cardZoneP1 false");
//			}
//			else if(((JPanel) e.getSource()).getName().equals("cardZoneP2")){//cardZoneP2
//				PokerFrame.cardZoneP2Enabled = false;
//				System.out.println("cardZoneP2 false");
//			}
//			else if(((JPanel) e.getSource()).getName().equals("cardZoneP3")){//cardZoneP3
//				PokerFrame.cardZoneP3Enabled = false;
//				System.out.println("cardZoneP3 false");
//			}
//			else if(((JPanel) e.getSource()).getName().equals("cardZoneP3")){//cardZoneP3
//				PokerFrame.cardZoneP4Enabled = false;
//				System.out.println("cardZoneP4 false");
//			}
//		}
	}
	
	public void retrieveAndProcessFinger(MouseEvent e, int type){
		indexOfFingerUnderMouse = fingerContainer.createFinger( e.getX(), e.getY() );
		Finger f = fingerContainer.getFingerByIndex( indexOfFingerUnderMouse );
		processMultitouchInputEvent(
			f.id,
			f.position.x(),
			f.position.y(),
			type
		);
		
	}
	
	public synchronized void processMultitouchInputEvent( int id, float x, float y, int type ) {
		//System.out.println("event: "+id+", "+x+", "+y+", "+type);
		int cursorIndex = cursorContainer.updateCursorById( id, x, y );

		if ( type == MultitouchFramework.TOUCH_EVENT_UP ) {
			// up event
			System.out.println("WM TOUCH EVENT UP");
			cursorContainer.removeCursorByIndex( cursorIndex );
		}
		else if ( type == MultitouchFramework.TOUCH_EVENT_MOVE ) {
			System.out.println(" WM TOUCH EVENT MOVE");
			if ( cursorContainer.getNumCursors() == 2 ) {
				System.out.println("2");
				MyCursor cursor0 = cursorContainer.getCursorByIndex( 0 );
				MyCursor cursor1 = cursorContainer.getCursorByIndex( 1 );
				gw.panAndZoomBasedOnDisplacementOfTwoPoints(
					id==cursor0.id ? cursor0.getPreviousPosition() : cursor0.getCurrentPosition(),
					id==cursor1.id ? cursor1.getPreviousPosition() : cursor1.getCurrentPosition(),
					cursor0.getCurrentPosition(),
					cursor1.getCurrentPosition()
				);
			}
			else if ( cursorContainer.getNumCursors() == 1 ) {
				MyCursor cursor = cursorContainer.getCursorByIndex( 0 );
				gw.pan(
					cursor.getCurrentPosition().x() - cursor.getPreviousPosition().x(),
					cursor.getCurrentPosition().y() - cursor.getPreviousPosition().y()
				);
			}
		}
		else if ( type == MultitouchFramework.TOUCH_EVENT_DOWN){
			System.out.println("WM TOUCH EVENT DOWN");
		}
	}

}
